<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/8/008
 * Time: 17:00
 */

namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\validate\IDMustBePostiveInt;
use app\api\validate\OrderFigureMeasureDataValidate;
use app\api\validate\PatternMasterValidate;
use app\api\model\PatternMaster as PatternMasterModel;
use app\api\service\PatternMaster as PatternMasterService;
use app\lib\exception\PatternMasterException;
use app\lib\exception\SuccessMessage;
use app\api\model\OrderFigureMeasureData as OrderFigureMeasureDataModel;

class PatternMaster extends BaseController {

    /**
     * 创建或更新量体师申请信息
     * @return \think\response\Json
     */
    public function createOrUpdatePatternMaster() {
        $patternMasterValidate = new PatternMasterValidate();
        $patternMasterValidate->goCheck();

        $param = $patternMasterValidate->getDataByRule(input('post.'));

        PatternMasterModel::createOrUpdatePatternMaster($param);

        return json(new SuccessMessage(), 201);
    }

    /**
     * 获取用户量体师数据
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws PatternMasterException
     */
    public function getPatternMasterInfo() {
        $pattern_master = PatternMasterModel::getPatternMasterInfo();
        if (!$pattern_master) {
            throw new PatternMasterException();
        }

        return $pattern_master;
    }

    /**
     * 接单
     * @return \think\response\Json
     */
    public function orderReceiving() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        PatternMasterService::orderReceiving($id);
        return json(new SuccessMessage(), 201);

    }

    /**
     * 确认量体完成
     * @param $id
     * @return \think\response\Json
     */
    public function affirmMeasure($id) {

        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        PatternMasterService::affirmMeasure($id);
        return json(new SuccessMessage(), 201);

    }

    /**
     * 创建或者更新用户订单量体数据
     * @return \think\response\Json
     */
    public function createOrUpdateFigureMeasure() {
        $orderFigureMeasureDataValidate = new OrderFigureMeasureDataValidate();
        $orderFigureMeasureDataValidate->goCheck();
        $params = $orderFigureMeasureDataValidate->getDataByRule(input('post.'));

        OrderFigureMeasureDataModel::createOrUpdateFigureMeasure($params);
        return json(new SuccessMessage(), 201);
    }
}